
// Issues
function addIssue(patientId) {
	if (!isNotEmpty($('textarea#issueValue'), _('Empty content'), 'statusMessageIssue')) {
		return;
	}	
	$('#addIssueBtn').css('display', 'none');
	$('#loading').css('display', 'block');
	var submitData = $('#addIssueForm').formSerialize();
	$.ajax ({
		type: 	"POST",
		url:	"addIssueAjax?set_ajax_view",
		data:	submitData + "&patientId=" + patientId,
		success: function(data) {
			$('#loading').css('display', 'none');
			$('#addIssueBtn').css('display', 'inline');
			if (data == 0) {
				$('span#statusMessageIssue').text(_('Duplicated personal info')).show();					
			} else {
				$('table#patientIssues tr#noresult').remove();
				$('table#patientIssues tbody').append(data);
			}
			$('#issueValue').val('');
		}
	});
}

function deleteIssue(id) {
	$.ajax ({
		type: 	"GET",
		url:	"deleteIssueAjax?set_ajax_view",
		data:	"patientIssueId=" + id,
		success: function(data) {
			$('table#patientIssues tr#' + id).remove();
			if ($('table#patientIssues tbody tr').length == 0) {
				$('table#patientIssues tbody').append("<tr id='noresult'><td colspan='5'>" + _("None of personal info is available.") + "</td></tr>");
			}		
		}
	});
}

function hideAlertMsg(target) {
	$('span#' + target).hide();
}

// Allergy
function addAllergy() {
	if (!checkDate($('input#date_admitted'), $('#admitted_mm'), $('#admitted_dd'), true, _('Empty time'), 'statusMessageAllergy', true)) {
		return;
	}
   	if (!isNotEmpty($('input#allergic_agent'), _('Empty allergic agent'), 'statusMessageAllergy')) {
   		return;
   	}
	$('#addAllergyBtn').css('display', 'none');
	$('#loading').css('display', 'block');
	var submitData = $('#addAllergyForm').formSerialize();
	$.ajax ({
		type: "POST",
		url:	"addAllergyAjax?set_ajax_view",
		data:	submitData + "&patient_id=" + $('#patient_id').val(),
		success: function(data) {
			$('#addAllergyBtn').css('display', 'inline');
			$('#loading').css('display', 'none');
			if (data == 0) {
				$('span#statusMessageAllergy').text(_('Duplicated allergic info')).show();
			} else {
				$('table#patientAllergy tr#noresult').remove();
				$('table#patientAllergy tbody').append(data);
			}
			$('#date_admitted').val('');
			$('#admitted_mm').val('');;
			$('#admitted_dd').val('');
			$('#allergic_agent').val('');
			$('#note').val('');
		}
	});
}

function deleteAllergy(id){
	$.ajax ({
		type: 	"GET",
		url:	"deleteAllergyAjax?set_ajax_view",
		data:	"patientAllergyId=" + id,
		success: function(data) {
			$('table#patientAllergy tr#' + id).remove();
			if ($('table#patientAllergy tbody tr').length == 0) {
				$('table#patientAllergy tbody').append("<tr id='noresult'><td colspan='5'>" + _("There is no allergic information") + "</td></tr>");
			}
		}
	});
}
// Chronic
function addChronic() {
	if (!checkDate($('input#chronic_date_admitted'), $('#chronic_admitted_mm'), $('#chronic_admitted_dd'), true, _('Empty time'), 'statusMessageChronic', true)) {
		return;
	}
	$('#addChronicBtn').css('display', 'none');
	$('#loading').css('display', 'block');
	var submitted	=	$('form#addChronicForm').formSerialize();
	$.ajax ({
		type: "POST",
		url:	"AddChronicAjax?set_ajax_view",
		data:	submitted,
		success: function(data) {
			$('#addChronicBtn').css('display', 'inline');
			$('#loading').css('display', 'none');
			if (data == "Duplicated") {
				$('span#statusMessageChronic').text(_('Duplicated chronic disease info')).show();
			} 
			else {
				$('table#patientChronics tr#noresult').remove();
				$('table#patientChronics tbody').append(data);
			}
			$('#chronic_date_admitted').val('');
			$('#chronic_admitted_mm').val('');
			$('#chronic_admitted_dd').val('');
			$('#chronic_note').val('');		
		}
	});
}

function deleteChronic(id){
	$.ajax ({
		type: "GET",
		url:	"deleteChronicAjax?set_ajax_view",
		data:	"patientChronicId=" + id,
		success: function(data) {
			$('table#patientChronics tr#' + id).remove();
			if ($('table#patientChronics tbody tr').length == 0) {
				$('table#patientChronics tbody').append("<tr id='noresult'><td colspan='6'>" + _("There is no chronic disease information") + "</td></tr>");
			}
		}
	});
}

// Other Disease
function addOtherDisease() {
	if (!checkDate($('input#disease_date_admitted'), $('#disease_admitted_mm'), $('#disease_admitted_dd'), true,  _('Empty time'), 'statusMessageDisease', true)) {
		return;
	}
   	if (!isNotEmpty($('input#disease_name'), _('Empty disease name'), 'statusMessageDisease')) {
   		return;
   	}
   	$('#addDiseaseBtn').css('display', 'none');
	$('#loading').css('display', 'block');	
	var submitData = $('#addOtherDiseaseForm').formSerialize();
	$.ajax ({
		type: 	"POST",
		url:	"addOtherDiseaseAjax?set_ajax_view",
		data:	submitData,
		success: function(data) {
		   	$('#addDiseaseBtn').css('display', 'inline');
			$('#loading').css('display', 'none');	
			if (data == "Duplicated") {
				$('span#statusMessageDisease').text(_('Duplicated disease info')).show();
			} 
			else {
				$('table#patientOtherDiseases tr#noresult').remove();
				$('table#patientOtherDiseases tbody').append(data);
			}
			$('#disease_admitted_dd').val('');
			$('#disease_admitted_mm').val('');			
			$('#disease_date_admitted').val('');
			$('#disease_name').val('');
			$('#disease_note').val('');	
		}
	});
}

function deleteOtherDisease(id){
	$.ajax ({
		type:	"GET",
		url:    "deleteOtherDiseaseAjax?set_ajax_view",
		data:	"patientOtherDiseaseId=" + id,
		success: function(data) {
			$('table#patientOtherDiseases tr#' + id).remove();
			if ($('table#patientOtherDiseases tbody tr').length == 0) {
				$('table#patientOtherDiseases tbody').append("<tr id='noresult'><td colspan='6'>" + _("There are no other diseases information") + "</td></tr>");
			}
		}
	});
}
// Surgery
function addSurgery() {
	if (!checkDate($('#surgery_date'), $('#surgery_date_mm'), $('#surgery_date_dd'), true, _('Empty time'), 'statusMessageSurgery', true)) {
		return;
	}
   	if (!isNotEmpty($('input#surgical_name'), _('Empty surgery name'), 'statusMessageSurgery')) {
   		return;
   	}
   	$('#addSurgeryBtn').css('display', 'none');
	$('#loading').css('display', 'block');
	var submitted	=	$('form#addSurgeryForm').formSerialize();
	$.ajax ({
		type: 	"POST",
		url:	"addSurgeryAjax?set_ajax_view",
		data:	submitted,
		success: function(data) {
			$('#addSurgeryBtn').css('display', 'inline');
			$('#loading').css('display', 'none');
			if (data == "Duplicated") {
				$('span#statusMessageSurgery').text(_('Duplicated surgery info')).show();
			} 
			else {
				$('table#patientSurgeries tr#noresult').remove();
				$('table#patientSurgeries tbody').append(data);
			}
			$('#surgery_date').val('');
			$('#surgery_date_mm').val('');
			$('#surgery_date_dd').val('');
			$('#surgical_name').val('');
			$('#surgery_note').val('');	
		}
	});
}

function deleteSurgery(id){
	$.ajax ({
		type: 	"GET",
		url:	"deleteSurgeryAjax?set_ajax_view",
		data:	"patientSurgeryId=" + id,
		success: function(data) {
			$('table#patientSurgeries tr#' + id).remove();
			if ($('table#patientSurgeries tbody tr').length == 0) {
				$('table#patientSurgeries tbody').append("<tr id='noresult'><td colspan='6'>" + _("There is no last surgical information") + "</td></tr>");
			}
		}
	});
}